﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.DataObjects.Networks
{
    public class NetworkDefinition
    {
        #region props
        private string _NetworkName;
        /// <summary></summary>
        public string NetworkName
        {
            get
            {
                return this._NetworkName;
            }
            set
            {
                this._NetworkName = value;
            }
        }

        private string _IconPath;
        /// <summary></summary>
        public string IconPath
        {
            get
            {
                return this._IconPath;
            }
            set
            {
                this._IconPath = value;
            }
        }

        private List<EntitySetProvider> _EntityProviders;
        /// <summary></summary>
        public List<EntitySetProvider> EntityProviders
        {
            get
            {
                return this._EntityProviders;
            }
            set
            {
                this._EntityProviders = value;
            }
        }

        private List<EntityRelationDefinition> _EntityRelations;
        /// <summary></summary>
        public List<EntityRelationDefinition> EntityRelation
        {
            get
            {
                return this._EntityRelations;
            }
            set
            {
                this._EntityRelations = value;
            }
        }
        #endregion

        public NetworkDefinition(string networkName, string iconPath)
        {
            this._NetworkName = networkName;
            this._IconPath = iconPath;
        }

        #region xml 
        public void Save(ref XmlDocument xDoc, XmlNode networkDefNode)
        {
            XmlDataUtil.UpdateAttribute(ref xDoc, networkDefNode,"NetworkName",this.NetworkName);
            XmlNode providerNodes = XmlDataUtil.AddElement(ref xDoc, networkDefNode, "EntityProviders");
            if(this.EntityProviders !=null && this.EntityProviders.Count>0)
            {
                foreach(EntitySetProvider entityProvider in this.EntityProviders)
                {
                    XmlNode providerNode = XmlDataUtil.AddElement(ref xDoc, providerNodes, "EntityProvider");
                    entityProvider.Save(ref xDoc, providerNode);
                }
            }
            XmlNode relationNodes = XmlDataUtil.AddElement(ref xDoc, networkDefNode, "EntityRelations");
            if(this.EntityRelation !=null && this.EntityRelation.Count>0)
            {
                foreach(EntityRelationDefinition entityRelationDefinition in this.EntityRelation)
                {
                    XmlNode relationNode = XmlDataUtil.AddElement(ref xDoc, relationNodes, "EntityRelation");
                    entityRelationDefinition.Save(ref xDoc, relationNode);
                }
            }
        }

        public static NetworkDefinition Read(XmlNode networkDefNode)
        {
            NetworkDefinition networkDef=new NetworkDefinition(string.Empty, string.Empty);
            networkDef.NetworkName = XmlDataUtil.GetAttributeValue(networkDefNode, "NetworkName", "");
            networkDef.EntityProviders=new List<EntitySetProvider>();
            XmlNodeList providerNodes = networkDefNode.SelectNodes("EntityProviders/EntityProvider");
            if(providerNodes !=null && providerNodes.Count>0)
            {
                foreach(XmlNode providerNode in providerNodes)
                {
                    EntitySetProvider provider = EntitySetProvider.Read(providerNode);
                    networkDef.EntityProviders.Add(provider);
                }
            }
            networkDef.EntityRelation=new List<EntityRelationDefinition>();
            XmlNodeList relationNodes = networkDefNode.SelectNodes("EntityRelations/EntityRelation");
            if(relationNodes !=null && relationNodes.Count>0)
            {
                foreach(XmlNode relationNode in relationNodes)
                {
                    EntityRelationDefinition relation = EntityRelationDefinition.Read(relationNode);
                    networkDef.EntityRelation.Add(relation);
                }
            }
            return networkDef;
        }
        #endregion
    }
}
